/*
 * 文件名：replyInfoController.java	 
 * 时     间：下午4:20:10
 * 作     者：liyf       
 * 版     权： 2014-2022  智慧园区, 公司保留所有权利.
 * 联     系：www.szyungu.com
 */
package com.cz073586.technology.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.cz073586.common.contants.CommonContans;
import com.cz073586.common.controller.BaseController;
import com.cz073586.common.entity.pojo.PageInfo;
import com.cz073586.technology.model.ReplyInfo;
import com.cz073586.technology.model.TopicInfo;
import com.cz073586.technology.model.UserInfo;
import com.cz073586.technology.service.face.ReplyInfoService;
import com.cz073586.technology.service.face.TopicInfoService;
import com.cz073586.technology.service.face.UserInfoService;
import com.cz073586.tools.privilege.ResourceAndOperate;
import com.cz073586.tools.util.JsonUtil;
import com.cz073586.tools.util.Tools;
import com.cz073586.tools.util.UUIDGenerateUtil;

/**
 * @ClassName: replyInfoController
 * @Description: 回复信息信息
 * @author zengqj
 * @date 2015年1月20日 下午4:20:10
 */
@RestController
@RequestMapping(value = "/replyInfoManager", method = RequestMethod.POST)
@ResourceAndOperate(
		moduleID = "com.technology.replyInfoManager", 
		moduleName = "回复信息管理", 
		resouceDesc = "留言回复管理",
		parentModuleID = "com.technology", 
		parentModuleName = "昱显科技项目"
		)
public class ReplyInfoController extends BaseController
{
	@Autowired
	private ReplyInfoService replyInfoService;
	
	@Autowired
	private UserInfoService userInfoService;
	
	@Autowired
	private TopicInfoService topicInfoService; 
	
	/**
	 * 
	 * @方法名称: queryServicesByPage
	 * @描述: 查询回复信息列表
	 *
	 * @参数:
	 * @param page
	 * @return
	 *
	 * @返回类型: ModelMap
	 * @作者:zengqj
	 * @可能抛出异常:
	 */
	@RequestMapping(value = "/queryReplyInfoByPage.action", consumes = "application/json", produces = "application/json")
	@ResponseBody
	@ResourceAndOperate(
			moduleID = "com.technology.replyInfoManager.queryReplyInfoListByPage", 
			moduleName = "查询", 
			resouceDesc = "查询分页对象",
			parentModuleID = "com.technology.replyInfoManager", 
			parentModuleName = "回复信息管理"
			)
	public ModelMap queryReplyInfoListByPage(@RequestBody PageInfo<ReplyInfo> page)
	{
		ModelMap resultMap = new ModelMap();
		
		try
		{
			replyInfoService.queryByPage(page);
			resultMap.put(CommonContans.STRUTS_DATA, page);
			resultMap.put(CommonContans.STRUTS_RESULT,
					CommonContans.STRUTS_SUCCESS);
		} catch (Throwable e)
		{
			// 请保持所有代码一直性，没有特殊处理请使用该代码.
			this.printExction(resultMap, e);
		}
		return resultMap;
	}
	
	@RequestMapping(value = "/queryReplyByTopic.action", consumes = "application/json", produces = "application/json")
	@ResponseBody
	@ResourceAndOperate(
			moduleID = "com.technology.replyInfoManager.queryReplyByTopic", 
			moduleName = "查询", 
			resouceDesc = "根据帖子id查询回复信息",
			parentModuleID = "com.technology.replyInfoManager", 
			parentModuleName = "回复信息管理"
			)
	public ModelMap queryReplyByTopic(@RequestBody ReplyInfo replyInfo)
	{
		ModelMap resultMap = new ModelMap();
		
		try
		{
			List<ReplyInfo> list = replyInfoService.queryReplyInfo(replyInfo);
			resultMap.put(CommonContans.STRUTS_DATA, list);
			resultMap.put(CommonContans.STRUTS_RESULT,
					CommonContans.STRUTS_SUCCESS);
		} catch (Throwable e)
		{
			// 请保持所有代码一直性，没有特殊处理请使用该代码.
			this.printExction(resultMap, e);
		}
		return resultMap;
	}
	
	/**
	 * 
	 * @方法名称: queryreplyInfoById
	 * @描述: 通过回复信息ID查询
	 *
	 * @参数:
	 * @param replyInfoId
	 * @return
	 *
	 * @返回类型: ModelMap
	 * @作者:zengqj
	 * @可能抛出异常:
	 */
	@RequestMapping(value = "/queryReplyInfoById.action", consumes = "application/json", produces = "application/json")
	@ResponseBody
	@ResourceAndOperate(
			moduleID = "com.technology.replyInfoManager.queryReplyInfoById", 
			moduleName = "查询单一对象", 
			resouceDesc = "查询单一对象",
			parentModuleID = "com.technology.replyInfoManager", 
			parentModuleName = "回复信息管理"
			)
	public ModelMap queryReplyInfoById(String replyInfoId)
	{
		ModelMap resultMap = new ModelMap();
		
		try
		{
			
			resultMap.put(CommonContans.STRUTS_DATA,
					replyInfoService.selectByPrimaryKey(replyInfoId));
			resultMap.put(CommonContans.STRUTS_RESULT,
					CommonContans.STRUTS_SUCCESS);
		} catch (Throwable e)
		{
			// 请保持所有代码一直性，没有特殊处理请使用该代码.
			this.printExction(resultMap, e);
		}
		return resultMap;
	}
	
	/**
	 * 
	 * 方法描述：添加回复信息信息，uuid由代码写入
	 * @Title: insertreplyInfo 
	 * @param @param replyInfo
	 * @param @return
	 * @return ModelMap
	 * @replyInfo replyInfoministrator
	 * 2015年1月21日
	 * @throws
	 */
	@RequestMapping(value = "/insertReplyInfo.action", consumes = "application/json", produces = "application/json")
	@ResponseBody
	@ResourceAndOperate(
			moduleID = "com.technology.replyInfoManager.insertReplyInfo", 
			moduleName = "添加", 
			resouceDesc = "",
			parentModuleID = "com.technology.replyInfoManager", 
			parentModuleName = "回复信息管理"
			)
	public ModelMap insertReplyInfo(@RequestBody ReplyInfo replyInfo)
	{
		ModelMap resultMap = new ModelMap();
		
		try
		{
			log.info("replyInfo:"+JsonUtil.getJsonString(replyInfo));
			replyInfo.setUuid(UUIDGenerateUtil.generate());
			resultMap.put(CommonContans.STRUTS_DATA,
					replyInfoService.insert(replyInfo));
			resultMap.put(CommonContans.STRUTS_RESULT,
					CommonContans.STRUTS_SUCCESS);
			//给用户加分
			UserInfo user = userInfoService.selectByPrimaryKey(replyInfo.getUserKey());
			Integer score = 0;
			if(null!=user&&!Tools.isEmpty(user.getScore()))
			{
				score = Integer.parseInt(user.getScore())+replyInfo.getScore();
			}else
			{
				score = replyInfo.getScore();
			}
			log.info("score:"+score);
			log.info("user:"+JsonUtil.getJsonString(user));
			user.setScore(score.toString());
			userInfoService.updateByPrimaryKey(user);
			//给帖子的回帖数+1
			TopicInfo topic = topicInfoService.selectByPrimaryKey(replyInfo.getTopicKey());
			if(null!=topic&&!Tools.isEmpty(topic.getCommentCount()))
			{
				topic.setCommentCount(topic.getCommentCount()+1);
			}else
			{
				topic.setCommentCount(1);
			}
			topicInfoService.updateByPrimaryKey(topic);
		} catch (Throwable e)
		{
			// 请保持所有代码一直性，没有特殊处理请使用该代码.
			this.printExction(resultMap, e);
		}
		return resultMap;
	}
	
	/**
	 * 
	 * 方法描述：根据replyInfo主机修改回复信息
	 * @Title: updatereplyInfo 
	 * @param @param replyInfo
	 * @param @return
	 * @return ModelMap
	 * @replyInfo replyInfoministrator
	 * 2015年1月21日
	 * @throws
	 */
	@RequestMapping(value = "/updateReplyInfo.action", consumes = "application/json", produces = "application/json")
	@ResponseBody
	@ResourceAndOperate(
			moduleID = "com.technology.replyInfoManager.updateReplyInfo", 
			moduleName = "修改", 
			resouceDesc = "",
			parentModuleID = "com.technology.replyInfoManager", 
			parentModuleName = "回复信息管理"
			)
	public ModelMap updateReplyInfo(@RequestBody ReplyInfo replyInfo)
	{
		ModelMap resultMap = new ModelMap();
		
		try
		{
			resultMap.put(CommonContans.STRUTS_DATA,
					replyInfoService.updateByPrimaryKey(replyInfo));
			resultMap.put(CommonContans.STRUTS_RESULT,
					CommonContans.STRUTS_SUCCESS);
		} catch (Throwable e)
		{
			// 请保持所有代码一直性，没有特殊处理请使用该代码.
			this.printExction(resultMap, e);
		}
		return resultMap;
	}
	
	/**
	 * 
	 * 方法描述：根据主键删除回复信息
	 * @Title: deletereplyInfo 
	 * @param @param replyInfoId
	 * @param @return
	 * @return ModelMap
	 * @replyInfo replyInfoministrator
	 * 2015年1月21日
	 * @throws
	 */
	@RequestMapping(value = "/deleteReplyInfo.action", consumes = "application/json", produces = "application/json")
	@ResponseBody
	@ResourceAndOperate(
			moduleID = "com.technology.replyInfoManager.deleteReplyInfo", 
			moduleName = "删除", 
			resouceDesc = "",
			parentModuleID = "com.technology.replyInfoManager", 
			parentModuleName = "回复信息管理"
			)
	public ModelMap deleteReplyInfo(String replyInfoId)
	{
		ModelMap resultMap = new ModelMap();
		
		try
		{
			resultMap.put(CommonContans.STRUTS_DATA,
					replyInfoService.deleteByPrimaryKey(replyInfoId));
			resultMap.put(CommonContans.STRUTS_RESULT,
					CommonContans.STRUTS_SUCCESS);
		} catch (Throwable e)
		{
			// 请保持所有代码一直性，没有特殊处理请使用该代码.
			this.printExction(resultMap, e);
		}
		return resultMap;
	}
}
